package model;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import model.Field.Directions;
import model.LevelInterpreter.Difficulties;
import utilities.PathFinder;

/**
 *
 * @author Misja
 */
public class Enemy extends Part implements ActionListener {

    private Difficulties difficulty;
    private PathFinder pathFinder;

    public Enemy(Image appereance, Difficulties difficulty) {
        super(appereance);
        super.setMovable(false);
        this.difficulty = difficulty;
        this.pathFinder = new PathFinder();
    }

    public ActionPreformed move(Difficulties difficulty) {
        ActionPreformed lastAction = ActionPreformed.NOACTION;
        switch (difficulty) {
            case EASY:
                lastAction = this.moveEasy();
                break;
            case MEDIUM:
            case HARD:
                lastAction = this.moveMedium();
                break;
        }
        return lastAction;
    }

    private ActionPreformed moveEasy() {
        Random random = new Random();
        ArrayList<Field> validOptions = new ArrayList<>();

        for (Directions direction : Directions.values()) {
            Field neighborField = this.currentField.getNeighbor(direction);

            if (neighborField != null) {
                if (neighborField.getPart() instanceof Player) {
                    currentField.setPart(null);
                    neighborField.setPart(this);
                    return ActionPreformed.PLAYERTERMINATED;
                } else if (neighborField.getPart() == null) {
                    validOptions.add(neighborField);
                }
            }
        }
 
        if (!validOptions.isEmpty()) {
            int x = random.nextInt((validOptions.size()));
            Field neighbor = validOptions.get(x);
         
            currentField.setPart(null);
            neighbor.setPart(this);
            return ActionPreformed.MOVED;
        }

        return ActionPreformed.CONTAINED;
    }

    private ActionPreformed moveMedium() {
        this.pathFinder.scan(super.currentField);
        if (this.pathFinder.hasValidPath()) {
            List<Field> path = this.pathFinder.getPath();
            currentField.setPart(null);
            Field nextField = path.get(path.size() - 2);
            if (nextField.getPart() instanceof Player) {
                nextField.setPart(this);
                return ActionPreformed.PLAYERTERMINATED;
            } else {
                nextField.setPart(this);
                return ActionPreformed.MOVED;
            }
        } else {
            return this.moveEasy();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ActionPreformed lastAction = this.move(this.difficulty);
        this.setChanged();
        this.notifyObservers(lastAction);
    }

    
}
